﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKRevitStairs.StairsModel.CommonUnit;
using YJKRevitStairs.StairsModel.DataInfos;

namespace YJKRevitStairs.StairsModel.Manage
{
	public class StairsRunCreator
	{
		public static StairsRun CreateStairsRun(ElementId stairsId, Autodesk.Revit.DB.Document doc, double bottomElevation, SingleRunCurveInfo runConstructionInfo)
		{
			return StairsRunCreator.CreateStraightRun(stairsId, doc, bottomElevation, runConstructionInfo);
		}

		private static StairsRun CreateSketchedRun(ElementId stairsId, Autodesk.Revit.DB.Document doc, double bottomElevation, SingleRunCurveInfo runConstructionInfo)
		{
			List<Curve> curvesByCurvesInfo = StairsRunCreator.GetCurvesByCurvesInfo(runConstructionInfo.RunBoundaryCurvesInfoList);
			List<Curve> curvesByCurvesInfo2 = StairsRunCreator.GetCurvesByCurvesInfo(runConstructionInfo.RunRiserCurvesInfoList);
			List<Curve> curvesByCurvesInfo3 = StairsRunCreator.GetCurvesByCurvesInfo(runConstructionInfo.RunPathCurvesInfoList);
			StairsRun result = StairsRun.CreateSketchedRun(doc, stairsId, bottomElevation, curvesByCurvesInfo, curvesByCurvesInfo2, curvesByCurvesInfo3);
			doc.Regenerate();
			return result;
		}

		private static StairsRun CreateStraightRun(ElementId stairsId, Autodesk.Revit.DB.Document doc, double bottomElevation, SingleRunCurveInfo runConstructionInfo)
		{
			double baseElevation = (doc.GetElement(stairsId) as Stairs).BaseElevation;
			List<Curve> curvesByCurvesInfo = StairsRunCreator.GetCurvesByCurvesInfo(runConstructionInfo.RunPathCurvesInfoList);
			if (curvesByCurvesInfo.Count == 0)
			{
				throw new ArgumentException("梯段路径为空");
			}
			if (curvesByCurvesInfo[0].GetType() != typeof(Line))
			{
				throw new ArgumentException("梯段路径不是直线");
			}
			Curve curve = curvesByCurvesInfo[0] as Line;
			Transform transform = Transform.CreateTranslation(new XYZ(0.0, 0.0, bottomElevation + baseElevation));
			Line line = curve.CreateTransformed(transform) as Line;
			List<Curve> curvesByCurvesInfo2 = StairsRunCreator.GetCurvesByCurvesInfo(runConstructionInfo.RunRiserCurvesInfoList);
			if (curvesByCurvesInfo2.Count == 0)
			{
				throw new ArgumentException("梯段踏步宽度错误");
			}
			if (curvesByCurvesInfo2[0].GetType() != typeof(Line))
			{
				throw new ArgumentException("梯段踏步宽度错误");
			}
			double length = curvesByCurvesInfo2[0].Length;
            StairsRunJustification stairsRunJustification = (StairsRunJustification)1;
			StairsRun stairsRun = StairsRun.CreateStraightRun(doc, stairsId, line, stairsRunJustification);
			doc.Regenerate();
			stairsRun.ActualRunWidth = length;
			return stairsRun;
		}

		public static void CreateSpiralStairsRun(ElementId stairsId, Autodesk.Revit.DB.Document doc, ArcRunData runData, bool lrMirror, bool tbMirror, XYZ ptCenter)
		{
			double num = runData.RunRadius - runData.RunWidth / 2.0;
			double num2 = runData.RunRotateAngle;
			double runIncludeAngle = runData.RunIncludeAngle;
			bool flag = runData.Clockwise;
			if (lrMirror)
			{
				flag = !flag;
				num2 = Geometry.formatAngle(-(num2 + Math.PI));
			}
			if (tbMirror)
			{
				flag = !flag;
				num2 = Geometry.formatAngle(-num2);
			}
            StairsRun.CreateSpiralRun(doc, stairsId, ptCenter, num, num2, runIncludeAngle, flag, (StairsRunJustification)1).ActualRunWidth = runData.RunWidth;
			doc.Regenerate();
		}

		private static List<Curve> GetCurvesByCurvesInfo(List<CurveInfo> curvesInfoList)
		{
			List<Curve> list = new List<Curve>();
			foreach (CurveInfo curveInfo in curvesInfoList)
			{
				list.Add(curveInfo.CurveObj);
			}
			return list;
		}
	}
}
